@echo off
REM Windows Build Script for Ollama Translator

echo 🚀 Building Ollama Translator for Windows...

REM Check if running on Windows
if not "%OS%"=="Windows_NT" (
    echo ❌ This script must be run on Windows
    exit /b 1
)

REM Check if Python is available
python --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Python not found. Please install Python 3.8+
    exit /b 1
)

REM Create build directories
if not exist build_windows mkdir build_windows
if not exist dist_windows mkdir dist_windows

REM Install dependencies
echo 📦 Installing dependencies...
python -m pip install --upgrade pip
python -m pip install requests PyQt6 PyInstaller

REM Build using PyInstaller with Windows-specific settings
echo 🔨 Building executable...
python -m PyInstaller ^
    --onefile ^
    --noconsole ^
    --name "ollama_translate" ^
    --workpath build_windows ^
    --distpath dist_windows ^
    --add-data "README.md;." ^
    --hidden-import config_manager ^
    --hidden-import ollama_client ^
    --hidden-import translator_gui ^
    --hidden-import config_window ^
    --hidden-import settings_dialog ^
    main.py

REM Create Windows installer script
echo 📦 Creating installer...
(
echo @echo off
echo REM Windows Installer for Ollama Translator
echo.
echo Installing Ollama Translator...
echo.

REM Check if running as admin
net session >nul 2>&1
if %errorlevel% equ 0 (
    echo Running as administrator
echo.
    REM Create program directory
    if not exist "%%ProgramFiles%%\Ollama Translator" mkdir "%%ProgramFiles%%\Ollama Translator"
    
    REM Copy executable
    copy "ollama_translate.exe" "%%ProgramFiles%%\Ollama Translator\"
    
    REM Create start menu shortcut
    set SHORTCUT="%%APPDATA%%\Microsoft\Windows\Start Menu\Programs\Ollama Translator.lnk"
    set TARGET="%%ProgramFiles%%\Ollama Translator\ollama_translate.exe"
    
    powershell -Command "$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut(%SHORTCUT%); $Shortcut.TargetPath = %TARGET%; $Shortcut.Save()"
    
    echo ✅ Installed to %%ProgramFiles%%\Ollama Translator
echo ✅ Start menu shortcut created
echo.
echo To run the application:
echo   Search for "Ollama Translator" in Start Menu
) else (
    echo Not running as administrator
echo.
    REM Create local app data directory
    if not exist "%%APPDATA%%\Ollama Translator" mkdir "%%APPDATA%%\Ollama Translator"
    
    REM Copy executable
    copy "ollama_translate.exe" "%%APPDATA%%\Ollama Translator\"
    
    echo ✅ Installed to %%APPDATA%%\Ollama Translator
echo.
echo To run the application:
echo   "%%APPDATA%%\Ollama Translator\ollama_translate.exe"
)

echo.
echo Installation complete!
echo.
pause
) > dist_windows\install_windows.bat

REM Create run script
(
echo @echo off
echo Starting Ollama Translator...
echo.
REM Check if Ollama is running
tasklist ^| findstr /i ollama >nul
if errorlevel 1 (
    echo ⚠️  Warning: Ollama service is not running
    echo Please start Ollama before using the translator
    echo.
)

start "" "ollama_translate.exe"
) > dist_windows\run.bat

REM Verify the build
if exist "dist_windows\ollama_translate.exe" (
    for /f %%i in ('dir "dist_windows\ollama_translate.exe" ^| find "ollama_translate.exe"') do set SIZE=%%i
    echo ✅ Build successful!
    echo 📁 Executable: dist_windows\ollama_translate.exe
    echo 📦 Installer: dist_windows\install_windows.bat
    echo.
    echo To run the application:
    echo   dist_windows\run.bat
    echo.
    echo To install system-wide:
    echo   Run dist_windows\install_windows.bat as Administrator
) else (
    echo ❌ Build failed
    exit /b 1
)

pause